package com.aaa.power.util;

import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: YuXin
 * @Date : Created in 2020/11/9 16:12
 * @ClassName : GlobalExceptionHandler
 * @Description : 全局异常处理器
 * @Package_Name : com.aaa.power.util
 */
//@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 通用异常处理方法  拦截所有异常  做统一处理
     *
     * @param e
     * @return Result
     */
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(Exception e) {
        e.printStackTrace();
        if (e instanceof CustomException) {
            CustomException ce = (CustomException) e;
            return new Result(ce.getErrorCode(), ce.getErrorMsg(), "自定义异常");
        }
        return new Result(ReturnCode.FAIL.getCode(), ReturnCode.FAIL.getMsg(), e.getClass().getName());
    }
}
